package com.nativetalk.service.three.teacher.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TsNtCourseInfo;
import com.nativetalk.bean.banner.TdNtBannerInfoIndex;
import com.nativetalk.bean.course.TsNtCourseInfoIndexTwo;
import com.nativetalk.bean.dict.TsNtDictDeatil;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.teacher.*;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/6/22.
 */
@Service
public class ThreeTeacherSearchServiceImpl implements ThreeTeacherSearchService {
    private static Logger logger = Logger.getLogger(ThreeTeacherSearchServiceImpl.class);
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Resource
    private TdNtCommentDao tdNtCommentDao;
    @Resource
    private TdNtReserveTimeDao tdNtReserveTimeDao;
    @Resource
    private TdNtBannerInfoDao tdNtBannerInfoDao;
    @Resource
    private TdNtTeacherPictureDao tdNtTeacherPictureDao;
    @Resource
    private TdNtTeacherLabelDao tdNtTeacherLabelDao;

    @Override
    public RetInfo findTeacherRomand() {
        String logInfo = this.getClass().getName() + ":findTeacherRomand:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            List<TdNtTeacherRoundRegister> teachers = tdNtTeacherDao.findTdNtTeacherRoundRegister();
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap =
                    (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);
            for (int i = 0; i < teachers.size(); i++) {
                if (teachers.get(i).getDict_id() != null) {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(teachers.get(i).getDict_id() + "");
                    teachers.get(i).setUniversity(tsNtDictDeatil.getCode_name());
                    teachers.get(i).setCountry(tsNtDictDeatilsMap.get(tsNtDictDeatil.getParent_id() + "").getCode_name());
                } else {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                    teachers.get(i).setUniversity(tsNtDictDeatil.getCode_name());
                    teachers.get(i).setCountry(tsNtDictDeatilsMap.get(tsNtDictDeatil.getParent_id() + "").getCode_name());
                }

            }
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(teachers);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo updateTeacherCollect(List<Map<String, Object>> list) {
        String logInfo = this.getClass().getName() + ":updateTeacherCollect:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> obj = list.get(i);
                int result = tdNtTeacherDao.findCollection(obj);
                if (result != 0) {
                    //更新
                    tdNtTeacherDao.updateTeacherCollect(obj);
                } else {
                    //插入
                    tdNtTeacherDao.insertTeacherCollect(obj);
                }
            }

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("操作成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findTeacherDetail(int tea_id, TdNtMember tdNtMember, int version) {
        String logInfo = this.getClass().getName() + ":findTeacherDetail:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             * 查找老师信息
             */
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap = (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);
            Map<String, Object> map = new HashMap<>();
            TdNtTeacherParticulars tdNtTeacherParticulars = tdNtTeacherDao.findTdNtTeacherParticulars(tea_id);
            /**
             * 查找老师标签 4.0
             */
            tdNtTeacherParticulars.setLabels(tdNtTeacherLabelDao.findTdNtTeacherListTeaIdCn(tdNtTeacherParticulars.getTea_id()));
            if (tdNtTeacherParticulars.getDict_id() != null) {
                TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherParticulars.getDict_id());
                tdNtTeacherParticulars.setDict_id(tsNtDictDeatil.getCode_name());
            } else {
                TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                tdNtTeacherParticulars.setDict_id(tsNtDictDeatil.getCode_name());
            }
            String country = tdNtTeacherParticulars.getNationality();
            if (country.equals("0") || country.equals("315")) {
                country = "1";
            }
            String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
            tdNtTeacherParticulars.setNationality(nationality);
            map.put("teacher_detail", tdNtTeacherParticulars);
            /**
             * 查找老师粉丝数
             */
            int funsnumber = tdNtTeacherDao.findFunsSize(tdNtTeacherParticulars.getTea_id());
            map.put("funsnumber", funsnumber);
            /**
             * 查找是否收藏老师 1未收藏0已收藏
             */
            String collection_flag = "1";
            //判断是否登录，没有登录默认为未收藏
            if (tdNtMember != null) {
                Map<String, Object> obj = new HashMap<String, Object>();
                obj.put("tea_id", tea_id);
                obj.put("memb_id", tdNtMember.getMemb_id());
                String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                if (flag != null) {
                    collection_flag = flag;
                }
            }
            map.put("collection_flag", collection_flag);

            map.put("picture_size", tdNtTeacherPictureDao.findPictureByTeaIdSize(tea_id));
            /**
             * 查找老师课程信息
             * 课程ID，name, image，其套餐的最小课时数，购买人数
             */
            List<TsNtCourseInfoIndexTwo> tsNtTeacherOfCourseInfoList = tdNtTeacherDao.findAllOpenCourse(tea_id);
            map.put("courses", tsNtTeacherOfCourseInfoList);


            map.put("teacher", Constants.teacher);

            /**
             * 查找评论 没有评论时不返回数据 前端该模块不显示
             */
            Page page = new Page();
            page.setObj(tea_id);
            int comment_size = tdNtCommentDao.findTdNtCommentByTeaIdSize(page);
            int weikeCommentSize = tdNtCommentDao.findWeikeCommentSizeByTeaId(page);
            map.put("comment_size", comment_size + weikeCommentSize);
            map.put("comments", new ArrayList<>());
            if (tdNtReserveTimeDao.findTdNtReserveTimeSize(tea_id) == 0) {
                map.put("is_reserve", 1);
            } else {
                map.put("is_reserve", 0);
            }

            if (tdNtTeacherParticulars.getNationality().equals("中国")) {
                BigDecimal min_amount = tdNtTeacherParticulars.getMin_amount().add(tdNtTeacherParticulars.getMin_amount().multiply(new BigDecimal(0.3)));
                tdNtTeacherParticulars.setMin_amount(min_amount.multiply(new BigDecimal(6.5)));
            } else {
                tdNtTeacherParticulars.setMin_amount(new BigDecimal("2"));
            }
            List<TdNtBannerInfoIndex> tdNtBannerInfos = tdNtBannerInfoDao.findTdNtBannerInfoIndexByType(3);
            map.put("video", tdNtBannerInfos.get(0).getImg_link());
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            map.put("share", tsNtWebSiteConfIndexMap.get("27").getData_value());
            map.put("share_name", tsNtWebSiteConfIndexMap.get("28").getData_value());
            map.put("share_url", Constants.share_teacher_url + tea_id);
            map.put("share_picutre", Constants.DOMAIN + "nativetalk/WechatIMG2.png");
            map.put("share_content", tsNtWebSiteConfIndexMap.get("28").getData_value());
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取教师详情页面数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error("==========error line:" + e.getStackTrace()[0].getLineNumber());
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findTeacher(int tea_id, TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":findTeacher:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             * 查找老师信息
             */
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap = (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);
            Map<String, Object> map = new HashMap<>();
            TdNtTeacherParticulars tdNtTeacherParticulars = tdNtTeacherDao.findTdNtTeacherParticulars(tea_id);
            if (tdNtTeacherParticulars.getDict_id() != null) {
                TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherParticulars.getDict_id());
                tdNtTeacherParticulars.setDict_id(tsNtDictDeatil.getCode_name());
            } else {
                TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                tdNtTeacherParticulars.setDict_id(tsNtDictDeatil.getCode_name());
            }
            String country = tdNtTeacherParticulars.getNationality();
            if (country.equals("0") || country.equals("315")) {
                country = "1";
            }
            String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
            tdNtTeacherParticulars.setNationality(nationality);
            TdNtTeacherLive tdNtTeacherLive = new TdNtTeacherLive();
            tdNtTeacherLive.setDict_id(tdNtTeacherParticulars.getDict_id());
            tdNtTeacherLive.setMin_amount(tdNtTeacherParticulars.getMin_amount());
            tdNtTeacherLive.setNationality(tdNtTeacherParticulars.getNationality());
            tdNtTeacherLive.setTea_head_portrait(tdNtTeacherParticulars.getTea_head_portrait());
            tdNtTeacherLive.setTea_id(tdNtTeacherParticulars.getTea_id());
            tdNtTeacherLive.setTea_label(tdNtTeacherParticulars.getTea_label());
            tdNtTeacherLive.setTea_name(tdNtTeacherParticulars.getTea_name());
            tdNtTeacherLive.setTea_score(tdNtTeacherParticulars.getTea_score());
            tdNtTeacherLive.setTea_sex(tdNtTeacherParticulars.getTea_sex());
            tdNtTeacherLive.setTea_total_times(tdNtTeacherParticulars.getTea_total_times());
            /**
             * 查找老师粉丝数
             */
            int funsnumber = tdNtTeacherDao.findFunsSize(tdNtTeacherParticulars.getTea_id());
            tdNtTeacherLive.setFunsnumber(funsnumber);
            /**
             * 查找是否收藏老师 1未收藏0已收藏
             */
            String collection_flag = "1";
            //判断是否登录，没有登录默认为未收藏
            if (tdNtMember != null) {
                Map<String, Object> obj = new HashMap<String, Object>();
                obj.put("tea_id", tea_id);
                obj.put("memb_id", tdNtMember.getMemb_id());
                String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                if (flag != null) {
                    collection_flag = flag;
                }
            }
//            tdNtReserveTimeDao.findTdNtReserveTimeList()
            tdNtTeacherLive.setCollection_flag(collection_flag);
            map.put("teacher_detail", tdNtTeacherLive);
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            map.put("share", tsNtWebSiteConfIndexMap.get("27").getData_value());
            map.put("share_name", tsNtWebSiteConfIndexMap.get("28").getData_value());
            map.put("share_url", Constants.share_teacher_url + tea_id);
            map.put("share_picutre", Constants.DOMAIN + "nativetalk/WechatIMG2.png");
            map.put("share_content", tsNtWebSiteConfIndexMap.get("28").getData_value());
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取教师详情页面数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findTeacherIndex(int tea_id) {
        String logInfo = this.getClass().getName() + ":findTeacherIndex:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取首页数据成功");
            TdNtTeacherIndex tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherIndex(tea_id);
            Map<String, Object> map = new HashMap<>();
            TdNtTeacherIndexThree tdNtTeacherIndexThree = new TdNtTeacherIndexThree();
            Page page = new Page();
            page.setObj(tea_id);
            int courseCommentSize = tdNtCommentDao.findTdNtCommentByTeaIdSize(page);
            int weikeCommentSize = tdNtCommentDao.findWeikeCommentSizeByTeaId(page);
            tdNtTeacherIndexThree.setComment_size(courseCommentSize + weikeCommentSize);
            tdNtTeacherIndexThree.setFuns_size(tdNtTeacherDao.findFunsSize(tea_id));
            tdNtTeacherIndexThree.setState(tdNtTeacherIndex.getState());
            tdNtTeacherIndexThree.setTea_balance(tdNtTeacherIndex.getTea_balance());
            tdNtTeacherIndexThree.setTea_head_portrait(tdNtTeacherIndex.getTea_head_portrait());
            tdNtTeacherIndexThree.setTea_name(tdNtTeacherIndex.getTea_name());
            tdNtTeacherIndexThree.setTea_score(tdNtTeacherIndex.getTea_score());
            tdNtTeacherIndexThree.setTea_total_times(tdNtTeacherIndex.getTea_total_times());

            map.put("tdNtTeacherIndex", tdNtTeacherIndexThree);
            if (tdNtTeacherIndex.getNationality() == 0) {
                map.put("nationality", 1);
            } else {
                map.put("nationality", 0);
            }

            // 老师是否有开启的课程
            List<TsNtCourseInfo> tsNtCategorylist = tdNtTeacherDao.findTeacherOpenCourseInfo(tea_id);
            if (tsNtCategorylist != null && tsNtCategorylist.size() > 0) {
                map.put("is_start_calss", 0);
            } else {
                map.put("is_start_calss", 1);
            }
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            map.put("share_name", tsNtWebSiteConfIndexMap.get("28").getData_value());
            map.put("pictures", tdNtTeacherPictureDao.findPictureByTeaIdSize(tea_id));
            map.put("share_url", Constants.share_teacher_url + tea_id);
            map.put("share_picutre", Constants.DOMAIN + tdNtTeacherIndex.getTea_head_portrait());
            map.put("share_content", tsNtWebSiteConfIndexMap.get("28").getData_value());
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo);
        return retInfo;
    }

    @Override
    public RetInfo findTeacher(int tea_id) {
        String logInfo = this.getClass().getName() + ":findTeacher:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             * 查找老师信息
             */
            Map<String, Object> map = new HashMap<>();
            TdNtTeacherParticulars tdNtTeacherParticulars = tdNtTeacherDao.findTdNtTeacherParticulars(tea_id);

            if (tdNtTeacherParticulars.getNationality().equals("197")) {
                BigDecimal min_amount = tdNtTeacherParticulars.getMin_amount().add(tdNtTeacherParticulars.getMin_amount().multiply(new BigDecimal(0.3)));
                tdNtTeacherParticulars.setMin_amount(min_amount.multiply(new BigDecimal(6.5)));
            } else {
                tdNtTeacherParticulars.setMin_amount(new BigDecimal("2"));
            }
            map.put("min_amount", tdNtTeacherParticulars.getMin_amount());
            map.put("state", tdNtTeacherParticulars.getState());
            if (tdNtReserveTimeDao.findTdNtReserveTimeSize(tea_id) == 0) {
                map.put("is_reserve", 1);
            } else {
                map.put("is_reserve", 0);
            }
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
